#include "main_window.h"
#include "ui_main_window.h"

void windowInit(QMainWindow* window);

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->label_img->setPixmap(QPixmap::fromImage(QImage(":/img/res/bg-1.png")));
    windowInit(this);

    installWindow = std::make_unique<InstallWindow>(nullptr);

    connect(ui->btn_close, &QPushButton::clicked, this, &MainWindow::OnClickClosed);
    connect(ui->btn_mini, &QPushButton::clicked, this, &MainWindow::OnClickMinimized);
    connect(ui->btn_install, &QPushButton::clicked, this, &MainWindow::OnClickInstall);

    imgList.push_back(":/img/res/bg-1.png");
    imgList.push_back(":/img/res/bg-2.png");
    imgList.push_back(":/img/res/bg-3.png");

    msgBox.setInformativeText("你真的要狠心离开我吗？ 退出后您将无法获得\n-高速下载安装特\n人工客服一对一服务\n现在购买，还可以获得6折优惠");
    msgBox.setStandardButtons(QMessageBox::Close | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Close);

    changeImg = new QTimer();
    changeImg->setInterval(5000);
    connect(changeImg, &QTimer::timeout, this, &MainWindow::OnChangeBgImg);
    changeImg->start();
}

MainWindow::~MainWindow()
{
    delete changeImg;
    delete ui;
}

void MainWindow::OnClickMinimized()
{
    if (activeCode == "114514-1919810") {
        this->showMinimized();
        return;
    }
    bool bRet = false;
    QString title = "最小化";
    QString label = "充值648获取激活码，解锁最小化功能！";
    activeCode = inputDialog.getText(nullptr, title, label, QLineEdit::Normal, "123456", &bRet);
    if (bRet) {
        if (!activeCode.isEmpty() && activeCode == "114514-1919810") {
            this->showMinimized();
            return;
        } else {
            QMessageBox::information(nullptr, tr("错误"), tr("你是一个一个一个错误激活码"), QMessageBox::Ok);
        }
    }
}

void MainWindow::OnClickClosed()
{
    switch (msgBox.exec()) {
    case QMessageBox::Close:
        QMessageBox::information(nullptr, tr("再见"), tr("燕子！没有你我可怎么活啊!"), QMessageBox::Close);
        this->close();
        break;
    default:
        QMessageBox::information(nullptr, tr("好欸"), tr("人家就知道你还是爱我的!"), QMessageBox::Close);
        break;
    }
}

void MainWindow::OnClickInstall()
{
    installWindow->show();
}

void MainWindow::OnChangeBgImg()
{
    auto image = new QImage();
    image->load(imgList[bgIndex].data());
    ui->label_img->setPixmap(QPixmap::fromImage(*image));

    bgIndex++;
    if (bgIndex == 3) {
        bgIndex = 0;
    }
}

void MainWindow::mousePressEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton) {
        // dragPosition = event->globalPos() - frameGeometry().topLeft();
        dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
        event->accept();
    } else if (event->button() == Qt::RightButton) {
        close();
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent* event)
{
    if (event->buttons() & Qt::LeftButton) {
        // move(event->globalPos()-dragPosition);//鼠标位置减去按下点的坐标
        move(event->globalPosition().toPoint() - dragPosition); //鼠标位置减去按下点的坐标
        event->accept();
    }
}

void windowInit(QMainWindow* window)
{
    //设置隐藏标题且圆角
    QBitmap bmp(window->size());
    bmp.fill();
    QPainter p(&bmp);
    p.setPen(Qt::NoPen);
    p.setBrush(Qt::black);
    p.drawRoundedRect(bmp.rect(), 15, 15);
    window->setMask((bmp));

    window->setMinimumSize(800, 600);
    window->setMaximumSize(800, 600);
    window->setWindowTitle("JingOS 安装助手");
    window->setWindowFlags(Qt::FramelessWindowHint);
}
